package com.ruoyi.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.IcanResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.CustomerDict;
import com.ruoyi.system.domain.ManeuveringPlatform;
import com.ruoyi.system.mapper.CustomerDictMapper;
import com.ruoyi.system.mapper.ManeuveringPlatformMapper;
import com.ruoyi.system.service.CustomerDictService;
import com.ruoyi.system.service.IManeuveringPlatformService;
import com.ruoyi.system.util.ChineseNumberConverter;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CustomerDictServiceImpl extends ServiceImpl<CustomerDictMapper, CustomerDict> implements CustomerDictService
//        , MetaObjectHandler
{

@Autowired
private CustomerDictMapper customerDictMapper;
    @Override
    public IcanResult insertZdk(CustomerDict customerDict) {
        customerDict.setZdjb("一级");
        customerDict.setPid("0");

        this.save(customerDict);
        //更新一下id路径
        customerDict.setIdPath(customerDict.getWdcId());
        this.updateById(customerDict);


        return IcanResult.success();
    }

    @Override
    public IcanResult insertZdkSon(CustomerDict customerDict) {


        this.save(customerDict);
            //更新路径
        CustomerDict dict = this.getById(customerDict.getPid());
        String idPath = dict.getIdPath();
        idPath+= (";"+customerDict.getWdcId());
        customerDict.setIdPath(idPath);


        int length = customerDict.getIdPath().split(";").length;
        String s = ChineseNumberConverter.toChinese(length);
        customerDict.setZdjb(s+"级");


        this.updateById(customerDict);


        return IcanResult.success();
    }

    @Override
    public IcanResult queryPage(CustomerDict customerDict) {

        List<CustomerDict> list = this.list();


        IPage<CustomerDict> ipage = new Page<>();
        if (customerDict.getPageNum()==null){
            customerDict.setPageNum(1);
        }
        if (customerDict.getPageSize()==null){
            customerDict.setPageSize(10);
        }
        ipage.setCurrent(customerDict.getPageNum());
        ipage.setSize(customerDict.getPageSize());

        LambdaQueryWrapper<CustomerDict> dictLambdaQueryWrapper = new LambdaQueryWrapper<>();

        //根据名称查询
        dictLambdaQueryWrapper.like(!StrUtil.isBlank(customerDict.getZdmc()),CustomerDict::getZdmc,customerDict.getZdmc());

        dictLambdaQueryWrapper.eq(CustomerDict::getPid,"e1e462c3d5b3404e06e449");

        dictLambdaQueryWrapper.orderByDesc(CustomerDict::getCreateDate);

        IPage<CustomerDict> result = customerDictMapper.selectPage(ipage, dictLambdaQueryWrapper);

        List<CustomerDict> records = result.getRecords();

        records.forEach(item->{
            String wdcId = item.getWdcId();
            List<CustomerDict> treeDateList = queryTreeDate(wdcId, list);
            item.setCustomerDictChild(treeDateList);
        });




        return IcanResult.success(result);

    }

    @Override
    public IcanResult queryTree() {


        List<CustomerDict> customerDictList = this.list();
        List<CustomerDict> treeDateList = queryTreeDate("0", customerDictList);

        return IcanResult.success(treeDateList);
    }

    @Override
    public IcanResult editMubiao(CustomerDict customerDict) {

        this.updateById(customerDict);

        return IcanResult.success();
    }

    @Override
    public IcanResult removeMubiao(String id) {

        //存储索要删除的id
        List<String> childIdList = new ArrayList<>();
        //获得所有需要删除的id
        this.queryChild(id,childIdList);
//        最后将传过来的Id添加进去
        childIdList.add(id);
        //批量删除
        customerDictMapper.deleteBatchIds(childIdList);
        return IcanResult.success();
    }

    private void queryChild(String id, List<String> childIdList) {
        /**  查询数据库pid等于id的数据！！！！
         * 如果你使用的是mybatis-plus则可以通过mybatis-plus查
         * 如果你用的是其他的，则通过其他的查询。
         */
        List<CustomerDict> List = customerDictMapper.selectList(
                new LambdaQueryWrapper<CustomerDict>().eq(CustomerDict::getPid, id)
        );
        //递归查询下一级Id，并且将上一级的Id存入到list中
        List.forEach(item->{
            childIdList.add(item.getWdcId());
            this.queryChild(item.getWdcId(),childIdList);
        });
    }


    //递归找到所有子类并封装到childList属性中
    private List<CustomerDict> queryTreeDate(String parentId, List<CustomerDict> treeDateList){
        return treeDateList.stream().filter(item->item.getPid().equals(parentId))
                .peek(item->item.setCustomerDictChild(queryTreeDate(item.getWdcId(),treeDateList))).collect(Collectors.toList());
    }



}
